package net.chenyan.server.controller.admapi;/**
 * @author chenyan
 * @createTime 2022/5/10 23:46
 * @description description
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.chenyan.commons.core.R;
import net.chenyan.server.po.ListMusic;
import net.chenyan.server.po.Music;
import net.chenyan.server.service.ListMusicService;
import net.chenyan.server.service.ListService;
import net.chenyan.server.service.MusicService;
import net.chenyan.server.service.UserListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 *@description: TODO
 *@author: chenyan
 *@create: 2022/5/10 23:46
 */
@Slf4j
@Api(tags = "音乐歌单接口")
@RestController
@RequestMapping(value = "/admapi/list")
public class ListController {
    @Autowired
    private ListService listService;
    @Autowired
    private ListMusicService listMusicService;
    @Autowired
    private MusicService musicService;
    @Autowired
    private UserListService userListService;


    @ApiOperation(value = "展示所有歌单")
    @PostMapping(value = "/getAllList")
    public R getAllList(@RequestParam(defaultValue = "0")int page, @RequestParam(defaultValue = "5")int size) {
        return R.data(listService.findAllList(PageRequest.of(page,size)));
}

    @ApiOperation(value = "按照热度排序歌单")
    @PostMapping(value = "/getListByLike")
    public R getListByLike(@RequestParam(defaultValue = "0")int page, @RequestParam(defaultValue = "5")int size) {
        return R.data(listService.findAllListByLike(PageRequest.of(page,size)));
    }

    @ApiOperation(value = "按照时间排序歌单")
    @PostMapping(value = "/getListByCreateTime")
    public R getListByCreateTime(@RequestParam(defaultValue = "0")int page, @RequestParam(defaultValue = "5")int size) {
        return R.data(listService.findAllListByCreateTime(PageRequest.of(page,size)));
    }

    @ApiOperation(value = "根据歌单获取歌曲歌单")
    @PostMapping(value = "/getListMusic")
    public R geListMusic(@RequestParam(defaultValue = "0")int page, @RequestParam(defaultValue = "5")int size,int listId) {
        QueryWrapper<ListMusic> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("list_id",listId);
        List<ListMusic> listMusics=listMusicService.list(queryWrapper);
        if(listMusics==null){
            return R.error("歌单信息空空如也");
        }
        List<Music> musicList=new ArrayList<>();
        for (int i = 0; i <listMusics.size() ; i++) {
            ListMusic listMusic=listMusics.get(i);
            int musicMusicId=listMusic.getMusicId();
            Music music=musicService.getById(musicMusicId);
            musicList.add(music);
        }
        PageHelper.startPage(page,size);
        PageInfo<List<Music>> pageInfo=new PageInfo(musicList);
        return R.data(pageInfo);
    }

    @ApiOperation(value = "根据歌单id显示歌单评论")
    @GetMapping ("/findListCommentById")
    public  R findListCommentById(@RequestParam(defaultValue = "0")int page, @RequestParam(defaultValue = "5")int size,int albumId) {
        return R.data(userListService.findAllListCommentById(PageRequest.of(page,size),albumId));
    }
}
